import PropTypes from 'prop-types'

import encodeJSONForScript from '../utils/encodeJSONForScript.js'
import { createElement as e } from 'react'
import { createScript as x } from '../utils/markup.js'

export default function MainTemplate ({
  title = 'UNPKG',
  description = 'The CDN for everything on npm',
  favicon = '/favicon.ico',
  data,
  content,
  elements = []
}) {
  return e(
    'html',
    { lang: 'en' },
    e(
      'head',
      null,
      e('meta', { charSet: 'utf-8' }),
      e('meta', { httpEquiv: 'X-UA-Compatible', content: 'IE=edge' }),
      description && e('meta', { name: 'description', content: description }),
      e('meta', {
        name: 'viewport',
        content: 'width=device-width,initial-scale=1,maximum-scale=1'
      }),
      e('meta', { name: 'timestamp', content: new Date().toISOString() }),
      favicon && e('link', { rel: 'shortcut icon', href: favicon }),
      e('title', null, title)
    ),
    e(
      'body',
      null,
      e('div', { id: 'root' }, content),
      data && x(`window.__DATA__ = ${encodeJSONForScript(data)}`),
      ...elements
    )
  )
}

if (process.env.NODE_ENV !== 'production') {
  const htmlType = PropTypes.shape({
    __html: PropTypes.string
  })

  MainTemplate.propTypes = {
    title: PropTypes.string,
    description: PropTypes.string,
    favicon: PropTypes.string,
    data: PropTypes.any,
    content: htmlType,
    elements: PropTypes.arrayOf(PropTypes.node)
  }
}
